package cn.iocoder.yudao.module.erp.controller.admin.dp.dpgz;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.erp.controller.admin.dispatchvehicle.vo.DispatchVehicleRespVO;
import cn.iocoder.yudao.module.erp.controller.admin.dispatchvehicle.vo.DispatchVehicleSaveReqVO;
import cn.iocoder.yudao.module.erp.controller.admin.dp.dpdn.vo.DpDnRespVO;
import cn.iocoder.yudao.module.erp.controller.admin.dp.dpgz.vo.DpGzPageReqVO;
import cn.iocoder.yudao.module.erp.controller.admin.dp.dpgz.vo.DpGzRespVO;
import cn.iocoder.yudao.module.erp.controller.admin.dp.dpgz.vo.DpGzSaveReqVO;
import cn.iocoder.yudao.module.erp.controller.admin.dp.dpyw.vo.DpYwPageReqVO;
import cn.iocoder.yudao.module.erp.controller.admin.dp.dpyw.vo.DpYwRespVO;
import cn.iocoder.yudao.module.erp.dal.dataobject.dispatchvehicle.DispatchVehicleDO;
import cn.iocoder.yudao.module.erp.dal.dataobject.dp.DpGzDO;
import cn.iocoder.yudao.module.erp.dal.dataobject.dp.DpYwDO;
import cn.iocoder.yudao.module.erp.dal.dataobject.dpitem.DpItemGzDO;
import cn.iocoder.yudao.module.erp.dal.dataobject.dplist.DpEastListGzDO;
import cn.iocoder.yudao.module.erp.dal.dataobject.dplist.DpWestListGzDO;
import cn.iocoder.yudao.module.erp.service.dp.DpGzService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;
import javax.validation.*;
import javax.servlet.http.*;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - ERP广州制单")
@RestController
@RequestMapping("/erp/dp-gz")
@Validated
public class DpGzController {

    @Resource
    private DpGzService dpGzService;

    @PostMapping("/create")
    @Operation(summary = "创建ERP广州制单")
//    @PreAuthorize("@ss.hasPermission('erp:dp-gz:create')")
    public CommonResult<String> createDpGz(@Valid @RequestBody DpGzSaveReqVO createReqVO) {
        return success(dpGzService.createDpGz(createReqVO));
    }

//    @PostMapping("/configCar")
//    @Operation(summary = "ERP广州运单配车")
//    @PreAuthorize("@ss.hasPermission('erp:dispatch-vehicle-gz:configcar')")
//    public CommonResult<Boolean> configCar(@RequestParam("dispatchNumber") String dispatchNumber,
//                                           @Valid @RequestBody List<DispatchorderitemGzSaveReqVO> createReqVO) {
//        return success(dpGzService.configCar(dispatchNumber,createReqVO));
//    }

    @PutMapping("/update")
    @Operation(summary = "更新ERP广州制单")
//    @PreAuthorize("@ss.hasPermission('erp:dp-gz:update')")
    public CommonResult<Boolean> updateDpGz(@Valid @RequestBody DpGzSaveReqVO updateReqVO) {
        dpGzService.updateDpGz(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除ERP广州制单")
    @Parameter(name = "id", description = "编号", required = true)
//    @PreAuthorize("@ss.hasPermission('erp:dp-gz:delete')")
    public CommonResult<Boolean> deleteDpGz(@RequestParam("id") String id) {
        dpGzService.deleteDpGz(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得ERP广州制单")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
//    @PreAuthorize("@ss.hasPermission('erp:dp-gz:query')")
    public CommonResult<DpGzRespVO> getDpGz(@RequestParam("id") String id) {
        DpGzDO dpGz = dpGzService.getDpGz(id);
        return success(BeanUtils.toBean(dpGz, DpGzRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得ERP广州制单分页")
//    @PreAuthorize("@ss.hasPermission('erp:dp-gz:query')")
    public CommonResult<PageResult<DpGzRespVO>> getDpGzPage(@Valid DpGzPageReqVO pageReqVO) {
        PageResult<DpGzDO> pageResult = dpGzService.getDpGzPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DpGzRespVO.class));
    }

    @GetMapping("/dpGzOuterList")
    @Operation(summary = "获得ERP广州制单出库列表")
//    @PreAuthorize("@ss.hasPermission('erp:dp-gz:outer')")
    public CommonResult<PageResult<DpGzRespVO>> dpGzOuterList(@Valid DpGzPageReqVO pageReqVO) {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        PageResult<DpGzDO> dpGzOuterList = dpGzService.dpGzOuterList(pageReqVO);
        return success(BeanUtils.toBean(dpGzOuterList, DpGzRespVO.class));
    }

    @GetMapping("/dpGzVehicleList")
    @Operation(summary = "获得ERP广州配车列表")
//    @PreAuthorize("@ss.hasPermission('erp:dp-gz:outer')")
    public CommonResult<PageResult<DpGzRespVO>> dpGzVehicleList(@Valid DpGzPageReqVO pageReqVO) {
        PageResult<DpGzDO> dpGzOuterList = dpGzService.dpGzOuterList(pageReqVO);
        return success(BeanUtils.toBean(dpGzOuterList, DpGzRespVO.class));
    }

    @PostMapping("/dpGzOutStroe")
    @Operation(summary = "ERP广州制单配车出库")
//    @PreAuthorize("@ss.hasPermission('erp:dp-gz:outer')")
    public CommonResult<Boolean> dpGzOutStroe(@Valid @RequestBody DispatchVehicleSaveReqVO createReqVO) {
        dpGzService.dpGzOutStroe(createReqVO);
        return success(true);
    }

    @GetMapping("/dpGzEnterList")
    @Operation(summary = "获得ERP广州制单入库列表")
//    @PreAuthorize("@ss.hasPermission('erp:dp-gz:enter')")
    public CommonResult<List<DispatchVehicleRespVO>> dpYwEnterList(@RequestParam(value = "dispatchNumber",required = false) String dispatchNumber,
                                                                   @RequestParam(value = "line",required = false) Integer line,
                                                                   @RequestParam(value = "dest",required = false) Integer dest,
                                                                   @RequestParam(value = "dpId",required = false) String dpId) {
        List<DispatchVehicleDO> dpGzEnterList = dpGzService.dpGzEnterList(dispatchNumber,line,1, dest,dpId);
        return success(BeanUtils.toBean(dpGzEnterList, DispatchVehicleRespVO.class));
    }

    @PostMapping("/enterStroeDpGz")
    @Operation(summary = "ERP广州运单入库")
//    @PreAuthorize("@ss.hasPermission('erp:dp-gz:enter')")
    public CommonResult<Boolean> enterStroeDpGz(@Valid @RequestBody List<DispatchVehicleSaveReqVO> createReqVO) {
        dpGzService.enterStroeDpGz(createReqVO);
        return success(true);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出ERP广州制单 Excel")
//    @PreAuthorize("@ss.hasPermission('erp:dp-gz:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportDpGzExcel(@Valid DpGzPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DpGzDO> list = dpGzService.getDpGzPage(pageReqVO).getList();
        // 对数据进行字段处理
        List<DpGzRespVO> processedList = list.stream().map(dpGzDO -> {
            DpGzRespVO respVO = BeanUtils.toBean(dpGzDO, DpGzRespVO.class);
            // 格式化创建时间
//            if (dpGzDO.getCreateTime() != null) {
//                respVO.setCreateTimeFormatted(dpGzDO.getCreateTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
//            }
            // 转换核销字段
            if (dpGzDO.getWriteOff() != null){
                respVO.setWriteOffFormatted(dpGzDO.getWriteOff() == 1 ? "是" : "否");
            }
            // 转换线路字段
            if (dpGzDO.getLine() != null) {
                respVO.setLineFormatted(dpGzDO.getLine() == 1 ? "东线" : "西线");
            }
            return respVO;
        }).collect(Collectors.toList());
        processedList.add(new DpGzRespVO());
        processedList.add(new DpGzRespVO());
        // 创建提示信息行，并将其添加到列表末尾
        DpGzRespVO footerRow = new DpGzRespVO();
        footerRow.setId("注意：" +
                "一：提货时双方当面交接清楚，如货物提走后发现损坏少货现象本公司不负责。\n" +
                "二：预期不提货者，产生一切费用及后果由客户自己承担。\n" +
                "三：易碎的货物在承运过程中发现破碎、丢失时，只赔偿丢失的货物，不赔偿破损的货物。\n" +
                "义乌库房地址:浙江省义乌市苏溪高君路7号(浙江博尼锦纶服份有限公司西)大门进去直走100米311库房 电话/Tel:15600000311(微信同号)\n" +
                "广州库房地址:广东省佛山市南海区里水镇河塱沙里水东部工业园10号南海毛中厂二期(东侧大院311库房) 电话/Te1:18500000311(微信同号)\n" +
                "新疆库房地址:新疆维吾尔自治区乌鲁木齐市沙依巴克区青葱路1048号附1号楼311库 电话/Te1:18199990311(微信同号)");
        // 将提示信息添加到列表末尾
        processedList.add(footerRow);
        // 导出 Excel
        ExcelUtils.write(response, "ERP广州制单.xls", "数据", DpGzRespVO.class, processedList);
//                        BeanUtils.toBean(processedList, DpYwRespVO.class));
    }

    @GetMapping("/export-list")
    @Operation(summary = "导出ERP广州制单东西线清单")
//    @PreAuthorize("@ss.hasPermission('erp:dp-gz:exportlist')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportDpGzExcelList(@Valid DpGzDO dpGzDO,HttpServletResponse response) throws IOException {
        //获取线路类型
        Integer  line = dpGzDO.getLine();
        List<DpEastListGzDO> dpEastList = dpGzService.getDpEastListGzListByNo(dpGzDO.getNo());
        List<DpWestListGzDO> dpWestList = dpGzService.getDpWestListGzListByNo(dpGzDO.getNo());
        if (line.equals(1)){
            // 导出 Excel东线清单
            ExcelUtils.write(response, "ERP广州制单东线清单.xls", "清单",DpEastListGzDO.class,
                    BeanUtils.toBean(dpEastList,DpEastListGzDO.class));
        }
        if (line.equals(2)){
            // 导出 Excel西线清单
            ExcelUtils.write(response, "ERP广州制单西线清单.xls", "清单",DpWestListGzDO.class,
                    BeanUtils.toBean(dpWestList,DpWestListGzDO.class));
        }
    }


    @PutMapping("/examine")
    @Operation(summary = "ERP物流广州制单审核")
//    @PreAuthorize("@ss.hasPermission('erp:dp-gz:examine')")
    public CommonResult<Boolean> examineDpGz(@Valid @RequestBody DpGzDO dpGzDO){
        dpGzService.examineDpGz(dpGzDO);
        return success(true);
    }

    // ==================== 子表（ERP广州制单货号详情） ====================

    @GetMapping("/dp-item-gz/list-by-no")
    @Operation(summary = "获得ERP广州制单货号详情列表")
    @Parameter(name = "no", description = "运单号")
//    @PreAuthorize("@ss.hasPermission('erp:dp-gz:detail')")
    public CommonResult<List<DpItemGzDO>> getDpItemGzListByNo(@RequestParam("no") String no) {
        return success(dpGzService.getDpItemGzListByNo(no));
    }

    // ==================== 子表（广州东线清单） ====================

    @GetMapping("/dp-east-list-gz/list-by-no")
    @Operation(summary = "获得广州东线清单列表")
    @Parameter(name = "no", description = "运单号")
//    @PreAuthorize("@ss.hasPermission('erp:dp-gz:detail')")
    public CommonResult<List<DpEastListGzDO>> getDpEastListGzListByNo(@RequestParam("no") String no) {
        return success(dpGzService.getDpEastListGzListByNo(no));
    }

    // ==================== 子表（广州西线清单） ====================

    @GetMapping("/dp-west-list-gz/list-by-no")
    @Operation(summary = "获得广州西线清单列表")
    @Parameter(name = "no", description = "运单号")
//    @PreAuthorize("@ss.hasPermission('erp:dp-gz:detail')")
    public CommonResult<List<DpWestListGzDO>> getDpWestListGzListByNo(@RequestParam("no") String no) {
        return success(dpGzService.getDpWestListGzListByNo(no));
    }

}